<?php
namespace Qgs\Controller;

class SupplierController extends BaseController
{

    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    // 列表
    public function index()
    {

        $where['local_id'] = $this->local_id;
        $order = "supplier_id desc";
        $qgs_supplier = M('qgs_supplier');
        $count = $qgs_supplier->where($where)->count();
        $Page = new \Think\Page($count, 20);
        $show = $Page->show();

        $supplier_mgs = $qgs_supplier->where($where)->order($order)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign([
            'supplier_mgs' => $supplier_mgs,
            'show' => $show
        ]);

        $this->display();
    }

    // 增加
    public function add(){

        if ($_POST) {
            $qgs_supplier = M('qgs_supplier');
            $data['code'] = I('code');
            $data['name'] = I('name');
            $data['full_name'] = I('full_name');
            $data['principal'] = I('principal');
            $data['addr'] = I('addr');
            $data['type'] = I('type');
            $data['phone'] = I('phone');
            $data['phone2'] = I('phone2');
            $data['email'] = I('email');
            $data['province'] = I('province');
            $data['city'] = I('city');
            $data['region'] = I('region');
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $data['contract_lift_time'] = I('contract_lift_time');
            $data['expres_template'] = I('expres_template');
            $data['local_id'] = $this->local_id;
            $res = $qgs_supplier->data($data)->add();

            if ($res) {
                $this->success('成功', '/Qgs/Supplier/index');
            }else{
                $this->error('失败');
            }

        }else{
            // 运费模板
            $dlytmplist = M('b2c_dlytmplist a')
                ->join(C('DB_PREFIX').'b2c_dlytmp b on a.tmp_id=b.tmp_id')
                ->field('a.*,b.active')
                ->where(['a.local_id' => $this->local_id, 'b.is_def'=>'1'])
                ->select();
            // 地区：省
            $regions = M('ectools_regions')->where(['region_grade' => 1])->select();
            $this->assign([
                'regions' => $regions,
                'dlytmplist' => $dlytmplist
            ]);
            $this->display();
        }
    }

    public function get_region(){
        $grade = I('grade');
        $region_id = I('region_id');
        $regions = M('ectools_regions')->where(['region_grade' => $grade,'p_region_id'=>$region_id])->select();
        echo json_encode($regions);
    }

    // 详情
    public function detail(){
        $id = I('id');
        $qgs_supplier = M('qgs_supplier');
        $supplier = $qgs_supplier->where(['supplier_id' => $id])->find();
        $regions = M('ectools_regions')->where(['region_grade' => 1])->select();

        if ($_POST) {

            $qgs_supplier = M('qgs_supplier');
            $data['code'] = I('code');
            $data['name'] = I('name');
            $data['full_name'] = I('full_name');
            $data['principal'] = I('principal');
            $data['addr'] = I('addr');
            $data['type'] = I('type');
            $data['phone'] = I('phone');
            $data['phone2'] = I('phone2');
            $data['email'] = I('email');
            $data['province'] = !empty(I('province')) ? I('province') : $supplier['province'];
            $data['city'] = !empty(I('city')) ? I('city') : $supplier['city'];
            $data['region'] = !empty(I('region')) ? I('region') : $supplier['region'];
//            $data['create_time'] = I('create_time');
            $data['contract_lift_time'] = I('contract_lift_time');
            $data['expres_template'] = I('expres_template');
            $data['local_id'] = $this->local_id;
            $res = $qgs_supplier->where(['supplier_id' => $id])->save($data);

            if ($res) {
                $this->success('成功', '/Qgs/Supplier/index');
            }else{
                $this->error('失败');
            }
            exit;
        }
// 运费模板
        $dlytmplist = M('b2c_dlytmplist a')
            ->join(C('DB_PREFIX').'b2c_dlytmp b on a.tmp_id=b.tmp_id')
            ->field('a.*,b.active')
            ->where(['a.local_id' => $this->local_id, 'b.is_def'=>'1'])
            ->select();
        $this->assign([
            'supplier' => $supplier,
            'regions' => $regions,
            'dlytmplist' => $dlytmplist
        ]);
        $this->display();
    }

    // 商品
    public function goods(){
        $id = I('id');

        $name = I('name', '', htmlspecialchars());

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['b.disabled'] = 'false'; // 货品没删除
        $where['a.store_status'] = 'true'; // 店铺上架

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";

        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();
        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();


        $qgs_supplier_store_product = M('qgs_supplier_store_product')->field('store_pid')->where(['supplier_id' =>$id])->select();
        $qgs_supplier_store_product = array_column($qgs_supplier_store_product, 'store_pid');



        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'id' => $id,
            'value' => $qgs_supplier_store_product,
        ]);

        $this->display();
    }

    // 保存商品
    public function goodsSave(){
        $id = $_GET['id'];

        $data = I('data', htmlspecialchars());

        $qgs_supplier_store_product = M('qgs_supplier_store_product');

        foreach ($data as $v) {
            $arr = explode('-', $v);
            if ($arr[1] == 1) {
                $qgs_supplier_store_product->add(['local_id' => $this->local_id, 'store_pid' => $arr[0], 'supplier_id' => $id]);
            }else {
                $qgs_supplier_store_product->where(['local_id' => $this->local_id, 'store_pid' => $arr[0], 'supplier_id' => $id])->delete();
            }
        }
    }

    /**
     * 删除供应商
     */
    public function delete(){
        $id = I('id');
        if (empty($id)) {
            $this->error('有误啊！');
        }
        $qgs_supplier_store_product = M('qgs_supplier');
        $res = $qgs_supplier_store_product->where(['local_id' => $this->local_id, 'supplier_id' => $id])->delete();
        if ($res) {
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

    //已选商品
    public function delete_goods(){
        $id = I('id');

        $name = I('name', '', htmlspecialchars());

        if (!empty($name)) {
            $where['b.name'] = array('like',"%$name%");
        }

        $page = max(I('get.p'), 1);
        $num = max(I('get.num'), 20);

        $order = "a.store_pid desc";
        $product_relation_model = M('b2c_store_product a');

        $where['a.local_id'] = session('userInfo')['local_id'];
        $where['b.disabled'] = 'false'; // 货品没删除
        $where['a.store_status'] = 'true'; // 店铺上架

        $field = "a.marketprice,a.price,a.store,a.discount,a.store_pid,b.product_id,b.goods_id,b.title,b.name,b.spec_info";
        $qgs_supplier_store_product = M('qgs_supplier_store_product')->field('store_pid')->where(['supplier_id' =>$id])->select();
        $qgs_supplier_store_product = array_column($qgs_supplier_store_product, 'store_pid');
        $supplier_list = implode(',', $qgs_supplier_store_product);
        $where['a.store_pid'] = ['in', $supplier_list];
        // 可售货品
        $count = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id')
            ->where($where)
            ->count();

        $Page = new \Think\Page($count, $num);
        $show = $Page->show();

        $product_relation_msg = $product_relation_model->join(C('DB_PREFIX') . 'b2c_products b on a.product_id=b.product_id', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->page($page, $num)
            ->select();

        $this->assign([
            'product_relation_msg' => $product_relation_msg,
            'show' => $show,
            'id' => $id,
            'value' => $qgs_supplier_store_product,
        ]);

        $this->display();
    }


}